<template>
  <div class="login-content">
    <div class="login-box">
      <div class="title">API管理平台登录</div>
      <div class="login-form">
        <el-form
          :model="loginForm"
          :rules="loginFormRules"
          ref="loginFormRef"
          label-width="0">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item class="login-btn">
            <el-button type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return false
        }
        const { data: res } = await this.$axios.post('login', this.loginForm)
        window.sessionStorage.setItem('token', `${res.token}`)
        window.sessionStorage.setItem('username', res.username)
        this.$router.push('/home/welcome')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login-content {
  height: 100%;
  background-color: #2b4b6b;
  .login-box {
    width: 450px;
    height: 350px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    background-color: #fff;
    .title {
      text-align: center;
      padding-top: 50px;
      font-size: 25px;
      color: #429488;
    }
    .login-form {
      position: absolute;
      width: 100%;
      padding: 0 15px;
      box-sizing: border-box;
      left: 0;
      bottom: 40px;
      .login-btn {
        .el-button {
          width: 100%;
          letter-spacing: 2px;
        }
      }
    }
  }
}
</style>
